﻿using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections;

/// <summary>
/// Locks input for everyone when there is mouse down.
/// Returns lock on mouse up.
/// 
/// Currently only works with UnityEngine.UI.Selectable.
/// </summary>
[RequireComponent(typeof(EventTrigger))]
public class BlockInput : MonoBehaviour
{
	public LockInput.LockType m_LockType = LockInput.LockType.HUD;

	private EventTrigger m_Trigger;

	private void Start()
	{
		m_Trigger = GetComponent<EventTrigger>();
		EventTrigger.Entry entry1 = new EventTrigger.Entry();
		entry1.eventID = EventTriggerType.PointerDown;
		entry1.callback = new EventTrigger.TriggerEvent();
		entry1.callback.AddListener((BaseEventData) => { MouseDown(); });

		EventTrigger.Entry entry2 = new EventTrigger.Entry();
		entry2.eventID = EventTriggerType.PointerUp;
		entry2.callback = new EventTrigger.TriggerEvent();
		entry2.callback.AddListener((BaseEventData) => { MouseUp(); });

		m_Trigger.delegates = new System.Collections.Generic.List<EventTrigger.Entry>();
		m_Trigger.delegates.Add(entry1);
		m_Trigger.delegates.Add(entry2);
	}

	public void MouseDown()
	{
		LockInput.AddLock(m_LockType);
	}

	public void MouseUp()
	{
		LockInput.RemoveLock(m_LockType);
	}
}
